package com.cgbl.cg.vm.home

import android.app.Application
import androidx.databinding.ObservableField
import api.CourseServiceFactory
import api.ProjectServiceFactory
import api.ResourceServiceFactory
import api.UserServiceFactory
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.configs.AppConstants
import com.robot.base.model.CourseAllTypeBean
import com.robot.base.model.ModuleBean
import com.robot.base.model.PageListBean
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent
import me.goldze.mvvmhabit.http.BaseResponse

class RecommendMasterCourseViewModel(application: Application) : GLBaseViewModel(application) {
    val type = ObservableField<Int>(AppConstants.BundleValue.MASTER_COURSE)
    val tabType = ObservableField(TAB_COURSE)
    val uc = UiChangeEvent()

    class UiChangeEvent {
        val tabType: SingleLiveEvent<Int> = SingleLiveEvent()
        val tabDataType: SingleLiveEvent<List<CourseAllTypeBean>> = SingleLiveEvent()
        val titleDataEvent = SingleLiveEvent<List<ModuleBean>>()
    }

    val clickSelectCourse = BindingCommand<Any>(BindingAction {
        tabType.set(TAB_COURSE)
        getTab()
        getModule("4")
    })

    val clickSelectResource = BindingCommand<Any>(BindingAction {
        tabType.set(TAB_RESOURCE)
        getTab()
        getModule("5")
    })

    fun initData() {
    }

    fun getModule(type: String) {
        UserServiceFactory.getModule(
            type
        ).subscribe(object :
            AbstractViewModelSubscriber<BaseResponse<List<ModuleBean>>>(this) {
            override fun onNext(t: BaseResponse<List<ModuleBean>>) {
                uc.titleDataEvent.postValue(t.data)
            }
        })
    }

    fun getTab() {
        when (type.get()) {
            AppConstants.BundleValue.MASTER_COURSE -> {
                //大师课程
                CourseServiceFactory.allType()
            }
            AppConstants.BundleValue.CLASS_CASE -> {
                //经典案例
                ProjectServiceFactory.allType()
            }
            AppConstants.BundleValue.POPULAR_RESOURCES -> {
                //热门资源
                ResourceServiceFactory.allType()
            }
            else -> {
                //VIP
                if(tabType.get() == TAB_COURSE){
                    CourseServiceFactory.allType()
                }else{
                    ResourceServiceFactory.allType()
                }
            }
        }.subscribe(object :AbstractViewModelSubscriber<BaseResponse<List<CourseAllTypeBean>>>(this){
            override fun onNext(t: BaseResponse<List<CourseAllTypeBean>>) {
                uc.tabDataType.postValue(t.data)
            }
        })
    }

    companion object {
        const val TAB_COURSE = 0

        const val TAB_RESOURCE = 1
    }
}